#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include "arch_pkg.h"



//static gboolean help = FALSE;
static char *dir_str = NULL;
static GString *target_dir = NULL;
static gboolean gb_unlink_en = FALSE;
static guint g_save_num = 1;
static GOptionEntry entries[] =
{
  { "cache_dir", 'c', 0, G_OPTION_ARG_STRING, &dir_str, "set cache dir", NULL },
  { "delete", 'd', 0, G_OPTION_ARG_NONE, &gb_unlink_en, "delete cache dir file", NULL },
  { "save_num", 's', 0, G_OPTION_ARG_INT, &g_save_num, "save new pkg file count", NULL },
  //{ "help", 0, 0, G_OPTION_ARG_NONE, &help, "help", NULL },
  { NULL }
};

#define DEFINE_MACRO(T_N, OBJECT_N, obj_n) \
    g_##obj_n##_get_type()


void g_hello_get_type()
{
    printf("test g_hello_get_type()\n");
}


int main(int argc, char *argv[])
{
    GList *pkg_list = NULL;
    GList *remove_list = NULL;
    char *p_str;
    char **p_test_pkg_strs ;
    struct stat statbuf;
    int dir_name_len;
    //--------------------------
    GArchPkg *p_arch_pkg;
    //GArchPkg *p_arch_pkg;
    //--------------------------
#if 0
const char *g_my_pkgs[6] = {
    "1.pkg.tar.xz", 
    "1.pkg.tar.gz", 
    "1.pkg.tar.bz2", 
    "1.pkg.bz2", 
    "1.aa.pkg.br2", 
    "1.aa.pkg.z2", 
};

    char **pp_pkgs_ptr ;
    char *p_pkgs_ptr ;

    int i = 0;
    g_printf("g_my_pkgs sizeof = %d\r\n", sizeof(g_my_pkgs)/ sizeof(const char *));
    pp_pkgs_ptr = g_my_pkgs;
    while (NULL != pp_pkgs_ptr) {
        if (NULL == *pp_pkgs_ptr) {
            break;
        }
        g_printf("i = %d\n", i);
        i++;
        pp_pkgs_ptr++;
    }//end while

    for (i = 0; i < sizeof(g_my_pkgs) / sizeof(char *); i++) {
        p_pkgs_ptr = g_my_pkgs[i];
        if (chk_is_arch_pkg_file(p_pkgs_ptr ) == 0) {
            g_printf("%s  this is pkg\r\n", p_pkgs_ptr);
        } else {
            g_printf("%s this is not pkg\r\n", p_pkgs_ptr);
        }
    }
    return 0;

    pp_pkgs_ptr = (char **)&g_my_pkgs[0];
    while (NULL != pp_pkgs_ptr) {
        if (NULL == *pp_pkgs_ptr) {
            break;
        }
        if (chk_is_arch_pkg_file(*pp_pkgs_ptr) == 0) {
            g_printf("%s  this is pkg\r\n", *pp_pkgs_ptr);
        } else {
            g_printf("%s this is not pkg\r\n", *pp_pkgs_ptr);
        }
        pp_pkgs_ptr++;
    }//end while

    return -1;
    #endif

    GError *error = NULL;
    GOptionContext *context;
    DEFINE_MACRO(A, B, hello);
    context = g_option_context_new ("- find old archlinux package");
    //g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
    g_option_context_add_main_entries (context, entries, NULL);
    //g_option_context_add_group (context, gtk_get_option_group (TRUE));
    if (!g_option_context_parse (context, &argc, &argv, &error)) {
        g_print ("option parsing failed: %s\n", error->message);
        exit (1);
     }

     g_option_context_free(context);

    if (0 == g_save_num) {
        g_printf("save file number can't set to zero!!!!\n");
        return -1;
    }

    if (NULL == dir_str) {
        target_dir = g_string_new("/var/cache/pacman/pkg");
    } else {
        dir_name_len = strlen(dir_str);
        if ((1 == dir_name_len) && ('/' == dir_str[dir_name_len - 1])) {
            g_free(dir_str);
            g_printf("error: count use / dir\n");
            return -1;
        } else if ('/' == dir_str[dir_name_len - 1]) {
            dir_str[dir_name_len - 1] = '\0';
        } else {
        }
        target_dir = g_string_new(dir_str);
        g_free(dir_str);
    }


    if (lstat(target_dir->str, &statbuf)) {
        if (S_ISDIR(statbuf.st_mode)) {
            // this is dir
        } else {
            g_printf("target_dir not is dir\nexit\n", target_dir->str);
            g_string_free(target_dir, FALSE);
            return -1;
        }
    }
    //-----------------------use gobject begin---------------------------
    #if 0
    g_type_init();
    p_arch_pkg = g_arch_pkg_new(); 
    g_arch_pkg_parse_by_str(p_arch_pkg , "virtualbox_bin-3.1.0-1-i686.pkg.tar.gz");
    g_arch_pkg_print_info(p_arch_pkg);
    g_arch_pkg_delete_delete_file(p_arch_pkg, target_dir->str, FALSE);
    g_object_unref(p_arch_pkg);
    g_string_free(target_dir, FALSE);
    return 0;
    #endif
    //-----------------------use gobject end---------------------------
    /*
    TArchPkg_t *p_arch_pkg;
    //g_printf("test_pkg_str = %s\n", test_pkg_str);
    p_test_pkg_strs = test_pkg_strs;
    while ((p_str = *p_test_pkg_strs++) != NULL) {
        g_printf("parse string = %s\n", p_str);
        p_arch_pkg = arch_pkg_new();
        init_arch_pkg(p_arch_pkg);
        if (parse_arch_pkg(p_arch_pkg, p_str) == 0) {
            g_printf("g_list_append() start\n");
            pkg_list = g_list_append(pkg_list, p_arch_pkg);
            g_printf("g_list_append() over\n");
        } else {
            arch_pkg_free(p_arch_pkg);
        }
        //print_arch_pkg(p_arch_pkg);
    }//end while
    */
    pkg_list = scan_pkgs(target_dir->str);
    if (NULL != pkg_list) { 
        /*  
        g_printf("find pkg_list list != NULL\n");
        show_all_list_pkg(pkg_list);
        free_all_list_pkg(pkg_list);
        g_list_free(pkg_list);
        */
          
        remove_list = find_remove_pkgs(pkg_list);
        if (NULL != remove_list) {
            g_printf("find remove list != NULL\n");
            ShowRemovePkgListInfo(remove_list);
            g_printf("-----------------\n\n\n");
            remove_list = FilterRemovePkgList(remove_list, g_save_num);
            printf("\n\n\n-----will remove package ----\n");
            //ShowRemovePkgListFileName(remove_list);
            //show_all_list_pkg(remove_list);
            DeleteRemovePkgListFileName(remove_list, target_dir->str, gb_unlink_en);
        } else {
            g_printf("find remove list == NULL\n");
        }
        free_scan_pkgs_list(remove_list);
        
        g_list_free(pkg_list);
    } else {
        g_printf("find pkg_list list == NULL\n");
    }
    g_string_free(target_dir, FALSE);


    return 0;
}

